//
//  PageTitleView.swift
//  DouYuSwift
//
//  Created by dingjunchuxing on 2019/7/3.
//  Copyright © 2019 text. All rights reserved.
//

import UIKit


private let scrollLineH : CGFloat = 2
private let kNormalColor : (CGFloat, CGFloat, CGFloat) = (85, 85, 85)
private let kSelectColor : (CGFloat, CGFloat, CGFloat) = (255, 128, 0)

protocol PageTitleViewDelegate : class{
    func pageTitleView(titleView : PageTitleView,selectIndex Index : Int)
}




class PageTitleView: UIView {

    var titles = [String]()
    
    var currentIndex : Int = 0
    
    weak var delegate : PageTitleViewDelegate?
    
    private lazy var scrollView : UIScrollView =  {
       let scroll = UIScrollView()
        
        scroll.showsVerticalScrollIndicator = false
        scroll.scrollsToTop = false
        scroll.bounces = false
        
        return scroll
    }()
    
    private lazy var titleLabels : [UILabel] = [UILabel]()
    
    private lazy var scrollLine : UIView = {
       let view = UIView()
       view.backgroundColor = UIColor.orange
      return view
    }()
    
    
    init(frame: CGRect, titles : [String]) {
        self.titles = titles;
        super.init(frame: frame);
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension PageTitleView{
    
    private func setupUI(){
        
        addSubview(scrollView)
        scrollView.frame = bounds
        
        //标题
        setupTitleLabel()
        
        //下划线
        setupTitleBottomLine()
        
    }
    
    private func setupTitleLabel (){
        
        let labelY = 0.0
        let labelW = bounds.width / CGFloat(titles.count)
        let labelH = bounds.height - scrollLineH
        for (index,value) in titles.enumerated() {
            
            let label = UILabel()
            label.font = UIFont.systemFont(ofSize: 16.0)
            label.textAlignment = .center
            label.text = value
            label.tag = index
            label.textColor = UIColor.lightGray
            
            let labelX = labelW * CGFloat(index)
            
            label.frame = CGRect(x: labelX, y: CGFloat(labelY), width: labelW, height: labelH)
            
            scrollView.addSubview(label)
            titleLabels.append(label)
            
            //添加手势
            label.isUserInteractionEnabled = true
            let tapG = UITapGestureRecognizer(target: self, action: #selector(self.titleLabelClick(_:)));
            
            label.addGestureRecognizer(tapG)
        }
    }
    
    private func setupTitleBottomLine (){
        
        //下边缘线
        let bottomLineH : CGFloat = 0.5
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor.lightGray
        bottomLine.frame = CGRect(x: 0, y: frame.height - bottomLineH, width: frame.width, height: bottomLineH)
        
        scrollView.addSubview(bottomLine)
        
        
        //滚动条
        guard let label = titleLabels.first else {
            return
        }
        label.textColor = UIColor.orange
        scrollView.addSubview(scrollLine)
        scrollLine.frame = CGRect(x: 0, y: frame.height - scrollLineH, width: label.frame.width, height: scrollLineH)
        
    }
    
}

extension PageTitleView {
    @objc fileprivate func titleLabelClick(_ tapGes : UITapGestureRecognizer){
        
        guard let curLabel = tapGes.view as? UILabel else {
            return
        }
        
        let oldLabel = titleLabels[currentIndex]
        
        oldLabel.textColor = UIColor.lightGray
        curLabel.textColor = UIColor.orange
        currentIndex = curLabel.tag
        let scrollLineX = CGFloat(currentIndex) * scrollLine.frame.width
        
        UIView.animate(withDuration: 0.1) {
            self.scrollLine.frame.origin.x = scrollLineX
        }
        
        delegate?.pageTitleView(titleView: self, selectIndex: currentIndex)
        
        
    }
}


// MARK:- 对外暴露的方法
extension PageTitleView {
    func setTitleWithProgress(_ progress : CGFloat, sourceIndex : Int, targetIndex : Int) {
        // 1.取出sourceLabel/targetLabel
        let sourceLabel = titleLabels[sourceIndex]
        let targetLabel = titleLabels[targetIndex]
        
        // 2.处理滑块的逻辑
        let moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x
        let moveX = moveTotalX * progress
        scrollLine.frame.origin.x = sourceLabel.frame.origin.x + moveX
        
        // 3.颜色的渐变(复杂)
        // 3.1.取出变化的范围
        let colorDelta = (kSelectColor.0 - kNormalColor.0, kSelectColor.1 - kNormalColor.1, kSelectColor.2 - kNormalColor.2)
        
        // 3.2.变化sourceLabel
        sourceLabel.textColor = UIColor(r: kSelectColor.0 - colorDelta.0 * progress, g: kSelectColor.1 - colorDelta.1 * progress, b: kSelectColor.2 - colorDelta.2 * progress)
        
        // 3.2.变化targetLabel
        targetLabel.textColor = UIColor(r: kNormalColor.0 + colorDelta.0 * progress, g: kNormalColor.1 + colorDelta.1 * progress, b: kNormalColor.2 + colorDelta.2 * progress)
        
        // 4.记录最新的index
        currentIndex = targetIndex
    }
}



